/*
 * Project it.battlehorse.rcp.tools.dbbase
 * Created on Nov 21, 2005
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * A copy of the LGPL is available also at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */ 
package it.battlehorse.rcp.tools.dbbase;


import java.io.Serializable;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.osgi.framework.Bundle;

/**
 * Collects all the properties needed to completely identify a database. 
 * The DbBase plugin read available databases from a file named 
 * {@code rdbms.properties }. Then each driver class specified in the
 * file is searched in the classpath. <br>
 * The resulting found driver class constitute the set of available databases
 * to the application.
 * 
 * @author battlehorse
 * @since Nov 21, 2005
 */
public class DbConfig implements Serializable {
	
	/**
	 * The serial version UID for serialization
	 */
	private static final long serialVersionUID = -7285018535112053418L;
	
	
	/**
	 * A symbolic name which identifies the database
	 */
	private String name;
	
	/**
	 * The JDBC driver class which is used to open new database connections
	 */
	private String driverClassName;
	
	/**
	 * The icon which is associated with this database
	 */
	private ImageDescriptor icon;
	
	/**
	 * The database id
	 */
	private String id;
	
	/**
	 * Create an explicitly unconfigured instance of the class. This constructor exists
	 * only for serialization / deserialization purposes.
	 *
	 */
	public DbConfig() {
	}	
	
	/**
	 * Creates a new instance of the class
	 * 
	 * @param id the database id, which must be unique among all databases
	 * @param name A symbolic name which identifies the database
	 * @param driverClassName The JDBC driver class
	 */
	public DbConfig(String id, String name, String driverClassName, ImageDescriptor icon) {
		this.id = id;
		this.name = name;
		this.driverClassName = driverClassName;
		this.icon = icon;
	}
	
	/**
	 * Creates a  new instance of the class, configuring it from a given extension point
	 * 
	 * @param element the extension point configuration
	 * @param namespace The namespace of the extension point which defines the database
	 */
	public DbConfig(IConfigurationElement element, String namespace) {
		this.id = element.getAttribute("id");
		this.name = element.getAttribute("name");
		this.driverClassName = element.getAttribute("driverClass");
		
		if (element.getAttribute("icon") != null) {			
			Bundle b = Platform.getBundle(namespace);
			if (b != null && b.getResource(element.getAttribute("icon")) != null)
				icon = ImageDescriptor.createFromURL(b.getResource(element.getAttribute("icon")));
			
			if (icon == null) {
				/* if we're unable to build the icon, use a default one */
				icon = DbBaseActivator.getImageDescriptor("resources/database.png");				
			}
		}   
		else {
			/* if the extension point does not specify an icon, use a default one */
			icon = DbBaseActivator.getImageDescriptor("resources/database.png");
		}		
	}

	/**
	 * Returns the JDBC driver class name
	 * 
	 * @return the JDBC driver class name
	 */
	public String getDriverClassName() {
		return driverClassName;
	}

	/**
	 * Returns the database symbolic name
	 * 
	 * @return the database symbolic name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * Returns the icon associated with this database
	 * 
	 * @return the icon associated with this database
	 */
	public ImageDescriptor getIcon() {
		return icon;
	}
	
	/**
	 * Returns the database id
	 * 
	 * @return the database id
	 */
	public String getId() {
		return id;
	}
	
	
	

}
